package com.example.service;

import com.example.entity.Hotel;
import com.example.entity.Room;
import com.example.mapper.HotelMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HotelService {

    @Resource
    private HotelMapper hotelMapper;

    @Resource
    private RoomService roomService;

    public void add(Hotel hotel) {
        hotelMapper.insert(hotel);
    }

    public void update(Hotel hotel) {
        hotelMapper.updateById(hotel);
    }

    public void deleteById(Long id) {
        hotelMapper.deleteById(id);
    }

    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            this.deleteById(id);
        }
    }

    public Hotel selectById(Long id) {
        return hotelMapper.selectById(id);
    }

    public List<Hotel> selectAll(Hotel hotel) {
        return hotelMapper.selectAll(hotel);
    }

    // 新增：查询酒店最低房间价格
    public Double getMinPriceByHotelId(Long hotelId) {
        return hotelMapper.getMinPriceByHotelId(hotelId);
    }

    // 扩展：支持starRating范围查询的分页方法（修改原selectPage）
    public PageInfo<Hotel> selectPage(Hotel hotel, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Hotel> list = hotelMapper.selectAll(hotel);  // hotel对象需包含minStarRating/maxStarRating字段
        return PageInfo.of(list);
    }

    // 获取房间信息
    public Room getRoomById(Long roomId) {
        if (roomId == null) {
            return null;
        }
        
        // 调用RoomService获取房间信息
        return roomService != null ? roomService.selectById(roomId) : null;
    }
}